namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class DebugSubmissionControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<DebugSubmissionController>()
                .DerivesFrom<Controller>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IRegisterRoutes>()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Allow("GET, HEAD, OPTIONS")
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new DebugSubmissionController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new DebugSubmissionController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new DebugSubmissionController() as IRegisterRoutes).Register(routes);

            Assert.Equal<int>(2, routes.Count);
        }

        [Fact]
        public void op_HtmlRepresentation()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                Token token = Token.New();

                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://example.com/submission/" + Token.New(),
                    xml = "<submission><data /></submission>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/debug/submission?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsType<DebugSubmissionResult>(new DebugSubmissionController().HtmlRepresentation());

                Assert.Equal<CultureInfo>(new CultureInfo("en"), Thread.CurrentThread.CurrentUICulture);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}